package com.zengdb.mybatis.service.impl;

import java.util.Collections;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.zengdb.mybatis.dao.UserMapper;
import com.zengdb.mybatis.domain.User;
import com.zengdb.mybatis.page.PageResult;
import com.zengdb.mybatis.query.QueryObject;
import com.zengdb.mybatis.service.IUserService;
import com.zengdb.mybatis.util.MyBatisUtil;

public class UserServiceImpl implements IUserService{

	@Override
	public PageResult<User> queryByCondition(QueryObject qo) {
		//获取session对象
		SqlSession session = MyBatisUtil.openSession();
		UserMapper dao = session.getMapper(UserMapper.class);
		//查询总记录数
		Integer count = dao.queryByConditionCount(qo);
		//当总记录数==0时，返回空的分页结果对象
		if(count.intValue() == 0){
			session.close();
			return new PageResult<User>(0, Collections.<User> emptyList(), 1, qo.getPageSize());
		}
		//当总记录数>0时，调用查询结果的方法,封分页结果对象并返回
		List<User> list = dao.queryByCondition(qo);
		session.close();
		return new PageResult<User>(count, list, qo.getCurrentPage(), qo.getPageSize());
	}

}
